package com.smart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.entity.Feedback;
import com.smart.entity.LoginUser;
import com.smart.mapper.FeedbackMapper;
import com.smart.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    FeedbackMapper feedbackMapper;

    @Override
    public Map<String, Object> FeedbackAll(Integer currentPage, Integer pageSize) {
        Page<Feedback> page = new Page<>(currentPage,pageSize);//查询的N页，显示N条数据
        IPage<Feedback> iPage = feedbackMapper.selectPage(page, null);
        Map<String, Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("current",iPage.getCurrent());
        map.put("data", iPage.getRecords());
        return map;
    }

    @Override
    public Integer FeedbacDetele(Integer id) {
        int i = feedbackMapper.deleteById(id);
        return i;
    }

    @Override
    public Feedback FeedbackBy(Integer id) {
        Feedback feedback = feedbackMapper.selectById(id);
        return feedback;
    }

    @Override
    public Integer FeedbackEdit(Feedback feedback) {
        QueryWrapper<Feedback> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", feedback.getId());
        Integer update = feedbackMapper.update(feedback, queryWrapper);
        return update;
    }

    @Override
    public Integer FeedbackAdd(Feedback feedback) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long userId = loginUser.getUsers().getUid();
        feedback.setFbUser(userId);
        feedback.setFbDate(new Date());
        int insert = feedbackMapper.insert(feedback);
        return null;
    }

}
